package com.hontech.libratest.activity.fragment.sub

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.hontech.libratest.R
import com.hontech.libratest.conn.DeviceConst
import com.hontech.libratest.model.DeviceStatus
import com.hontech.libratest.utils.isBitSet

class StatusSub(view: View) {

    private val mTv18b20Temp = view.findViewById<TextView>(R.id.id_status_tv_18b20)
    private val mTvCh12 = view.findViewById<TextView>(R.id.id_status_tv_ch12)
    private val mTvCh13 = view.findViewById<TextView>(R.id.id_status_tv_ch13)
    private val mTvPushMotor = view.findViewById<TextView>(R.id.id_status_tv_push_motor)
    private val mTvElevator = view.findViewById<TextView>(R.id.id_status_tv_elevator)

    private val mTvSws = arrayOf(
        view.findViewById<TextView>(R.id.id_status_tv_door_close), // 0
        view.findViewById<TextView>(R.id.id_status_tv_push_open), // 1
        view.findViewById<TextView>(R.id.id_status_tv_push_close), // 2
        view.findViewById<TextView>(R.id.id_status_tv_elock_close), // 3
        view.findViewById<TextView>(R.id.id_status_tv_elevator_origin), // 4
        view.findViewById<TextView>(R.id.id_status_tv_col1_check), // 5
        view.findViewById<TextView>(R.id.id_status_tv_col2_check), // 6
        view.findViewById<TextView>(R.id.id_status_tv_col3_check), // 7
        view.findViewById<TextView>(R.id.id_status_tv_col4_check), // 8
        view.findViewById<TextView>(R.id.id_status_tv_col5_check), // 9
        view.findViewById<TextView>(R.id.id_status_tv_col6_check), // 10
        view.findViewById<TextView>(R.id.id_status_tv_col7_check), // 11
        view.findViewById<TextView>(R.id.id_status_tv_col8_check), // 12
        view.findViewById<TextView>(R.id.id_status_tv_col9_check), // 13
        view.findViewById<TextView>(R.id.id_status_tv_col10_check), // 14
        view.findViewById<TextView>(R.id.id_status_tv_hum), // 15
        view.findViewById<TextView>(R.id.id_status_tv_raster), // 16
        view.findViewById<TextView>(R.id.id_status_tv_pick_check), // 17
        view.findViewById<TextView>(R.id.id_status_tv_pick2_check), // 18
        view.findViewById<TextView>(R.id.id_status_tv_18b20_status), // 19
        view.findViewById<TextView>(R.id.id_status_tv_elevator_status), // 20
    )

    fun update() {
        val status = DeviceStatus.instance
        val sw = status.sw2.value

        mTv18b20Temp.text = "${status.temp.signedValue()}℃"
        mTvCh12.text = "${status.ch12.value}"
        mTvCh13.text = "${status.ch13.value}"
        mTvPushMotor.text = DeviceConst.msgWithCode(status.pushMotor.value)
        mTvElevator.text = DeviceConst.msgWithCode(status.elevator.value)

        for (i in 0..20) {
            val ret = sw.isBitSet(i)
            mTvSws[i].text = if (ret) "有效" else "无效"
            mTvSws[i].setTextColor(if (ret) Color.BLUE else Color.RED)
        }
    }
}

